package com.liu.qianfeng.day31_expandablelistview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private ExpandableListView expandableListView;
    private String[] groups = {"家人","朋友","同事"};
    private String[][] childNames = {{"曹操","夏侯惇","许褚"},{"刘备","诸葛亮","赵云","关羽"},{"孙权","黄盖","大乔"}};
    private int[][] childImages = {{R.mipmap.image_info1,R.mipmap.image_info2,R.mipmap.image_info5},{R.mipmap.image_info3,R.mipmap.image_info4,R.mipmap.image_info7,R.mipmap.image_info9},{R.mipmap.image_info6,R.mipmap.image_info8,R.mipmap.image_info10}};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 初始化可拓展视图
        expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
        expandableListView.setGroupIndicator(null);//去掉ExpandableListView默认的指示器你箭头
        //给可拓展视图设置适配器
        expandableListView.setAdapter(new MyAdapter());
        expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                Toast.makeText(MainActivity.this,groups[groupPosition]+"被点击了",Toast.LENGTH_SHORT).show();
                return false;
            }
        });

        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
               Toast.makeText(MainActivity.this,childNames[groupPosition][childPosition]+"被点击了",Toast.LENGTH_SHORT).show();
                return false;
            }
        });

        expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                Toast.makeText(MainActivity.this,"组被展开了",Toast.LENGTH_SHORT).show();
            }
        });

        expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
            @Override
            public void onGroupCollapse(int groupPosition) {
                Toast.makeText(MainActivity.this,"组被折叠了",Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 自定义ExpandableListView的适配器
     */
    public class MyAdapter extends BaseExpandableListAdapter{
        /**
         * 返回当前组的数目
         * @return
         */
        @Override
        public int getGroupCount() {
            return groups.length;
        }

        /**
         * 根据指定组的下标 获取当前组中child item的个数
         * @param groupPosition
         * @return
         */
        @Override
        public int getChildrenCount(int groupPosition) {
            return childNames[groupPosition].length;
        }

        /**
         * 根据指定组的位置获取当前组的对象
         * @param groupPosition
         * @return
         */
        @Override
        public Object getGroup(int groupPosition) {
            return groups[groupPosition];
        }

        /**
         * 根据指定组的下标和child的下标 获取指定child item对象
         * @param groupPosition
         * @param childPosition
         * @return
         */
        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return childImages[groupPosition][childPosition];
        }

        /**
         * 根据指定位置获取当前组的id
         * @param groupPosition
         * @return
         */
        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        /**
         * 组和子元素是否持有稳定的id
         * @return
         */
        @Override
        public boolean hasStableIds() {
            return true;
        }

        /**
         * 返回当前组的View视图
         * @param groupPosition：当前绘制组的下标
         * @param isExpanded 当前组是否可以展开
         * @param convertView 优化时可复用的视图对象
         * @param parent 父布局
         * @return
         */
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_group,null);
            ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
            TextView textView = (TextView) view.findViewById(R.id.textView);
            textView.setText(groups[groupPosition]);//设置组的名称
            if (isExpanded) {// 当前组是展开的 使用向下的箭头图片
                imageView.setImageResource(R.mipmap.arrow_down);
            }else{
                imageView.setImageResource(R.mipmap.arrow_right);
            }
            return view;
        }

        /**
         * 绘制每个child 的View对象
         * @param groupPosition
         * @param childPosition
         * @param isLastChild
         * @param convertView
         * @param parent
         * @return
         */
        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_child,null);
            ImageView imageView_child = (ImageView) view.findViewById(R.id.imageView_child);
            TextView textView_child = (TextView) view.findViewById(R.id.textView_child);
            imageView_child.setImageResource(childImages[groupPosition][childPosition]);
            textView_child.setText(childNames[groupPosition][childPosition]);
            return view;
        }

        /**
         * 表示组中child item 是否能够被选择
         * @param groupPosition
         * @param childPosition
         * @return
         */
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {

            return true;
        }
    }
}
